package com.yike.user.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yike.common.exception.BusinessException;
import com.yike.common.redis.RedisUtil;
import com.yike.common.tool.HttpsRequestUtil;
import com.yike.common.tool.StringUtil;
import com.yike.user.param.GetAppCodeParam;
import com.yike.user.vo.wx.WxAccessTokenVo;
import com.yike.user.vo.wx.WxAppLoginVo;
import com.yike.user.vo.wx.WxPhoneVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Sakura
 * @date 2024/8/22 11:04
 * 微信小程序工具类
 */
@Component
@Slf4j
public class WxAgentAppUtil {

    @Value("${wx.app.agent-appid}")
    private String appid;

    @Value("${wx.app.agent-secret}")
    private String secret;

    // 获取授权token
    private static final String GET_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token";

    // 获取授权用户信息URL
    private static final String GET_USER_INFO = "https://api.weixin.qq.com/sns/userinfo";

    @Autowired
    private RedisUtil redisUtil;

    // 移动APP微信授权登录
    public WxAppLoginVo login(String code) {
        try {
            // 先获取access_token
            // 拼接请求参数
            StringBuilder strTokenUrl = new StringBuilder();
            strTokenUrl.append(GET_TOKEN)
                    .append("?grant_type=authorization_code")
                    .append("&appid=").append(appid)
                    .append("&code=").append(code)
                    .append("&secret=").append(secret);

            String resultTokenStr = HttpsRequestUtil.sendGetRequest(strTokenUrl.toString());
            log.info("【移动APP获取微信授权凭证】：返回结果：" + resultTokenStr);
            WxAccessTokenVo wxAccessTokenVo = JSON.parseObject(resultTokenStr, WxAccessTokenVo.class);
            if (StringUtil.isBlank(wxAccessTokenVo.getAccess_token())) {
                throw new BusinessException("移动APP获取微信授权凭证异常");
            }
            // 获取用户信息
            // 拼接请求参数
            StringBuilder strUserInfoUrl = new StringBuilder();
            strUserInfoUrl.append(GET_USER_INFO)
                    .append("?access_token=").append(wxAccessTokenVo.getAccess_token())
                    .append("&openid=").append(wxAccessTokenVo.getOpenid());

            String resultUserInfoStr = HttpsRequestUtil.sendGetRequest(strUserInfoUrl.toString());
            log.info("【移动APP获取微信用户信息】：返回结果：" + resultUserInfoStr);
            WxAppLoginVo wxAppLoginVo = JSON.parseObject(resultUserInfoStr, WxAppLoginVo.class);
            if (StringUtil.isBlank(wxAppLoginVo.getOpenid())) {
                throw new BusinessException("移动APP获取微信授权凭证异常");
            }
            return wxAppLoginVo;
        } catch (Exception e) {
            log.info("【移动APP获取微信授权凭证】：获取异常：" + e.getMessage());
            e.printStackTrace();
            throw new BusinessException("移动APP获取微信授权凭证");
        }

    }
}
